/**
 * 个人信息储存
 *
 **/
import {defineStore} from "pinia";
import {UserInfoType} from "./interface";

export const useUserInfo = defineStore(
    'userinfo',
    {
        state: (): UserInfoType => {
            return {
                avatar: '',
                id: 0,
                lastlogintime: '',
                nickname: '',
                refreshToken: '',
                token: '0ac263dd-6856-45fb-9461-b635cd339e3e',
                username: ''
            }
        },
        actions: {
            dataFill(userinfo: UserInfoType) {
                this.$state = userinfo
            },
            removeToken() {
                this.token = ''
                this.refreshToken = ''
            },
            getToken(type: 'auth' | 'refresh' = 'auth') {
                return type === 'auth' ? this.token : this.refreshToken
            },
        },
        persist: {
            enabled: true,
            strategies: [
                {
                    key: 'useUserInfo',
                    storage: localStorage
                }
            ]
        }
    }
)